<?php
	require_once('includes.php');
	Session::start();
	
	//This function is used in for logging in
	
	//Log in
	if (isset($_POST['login'])) {
		//Check for username and password
		if (isset($_POST['username']) && isset($_POST['password'])) {
			$user_username = $_POST['username'];
			$user_password = $_POST['password'];					
			
			//Check username and password to the database
			$login = User::compare($user_username, $user_password); 
			//Find ID given username
			$user_id = User::findID($user_username);
		} else {
			//echo "Username and password POST not found <br />";
		}
		
		//Check if log in
		//If not logged in, send error
		if ($login === 0) {
			$error[] = "ERROR_LOGIN_INVALID";
			$_SESSION['message'] = $error;
			header('Location: ' . DIR . '/index.php');
		} else {		
			$session = new Session($user_id);
			$_SESSION['session'] = serialize($session);
			header('Location: ' . DIR . '/index.php');
		}	
	}
?>